package com.txd.mofa.shouye.aty;


import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.kongzue.baseframework.interfaces.Layout;
import com.kongzue.baseframework.util.JumpParameter;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.ResponseListener;
import com.kongzue.baseokhttp.util.Parameter;
import com.ninetaildemonfox.zdl.ninetails.map.JSONUtils;
import com.txd.mofa.R;
import com.txd.mofa.adapter.ShaixuanAdapter;
import com.txd.mofa.adapter.ShaixuanAdapter1;
import com.txd.mofa.adapter.ShaixuanAdapter2;
import com.txd.mofa.base.BaseAty;
import com.txd.mofa.bean.ShaixuanBean;
import com.txd.mofa.net.NetUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Layout(R.layout.aty_shaixuan)
public class ShaiXuanAty extends BaseAty {

    private ImageView iv_back,iv_ok;
    private RecyclerView recy,recy1,recy2;
    private ShaixuanAdapter shaixuanAdapter;
    private ShaixuanAdapter1 shaixuanAdapter1;
    private ShaixuanAdapter2 shaixuanAdapter2;

    @Override
    public void initViews() {
        super.initViews();

        iv_back = findViewById(R.id.iv_back);
        iv_ok = findViewById(R.id.iv_ok);
        recy = findViewById(R.id.recy);
        recy1 = findViewById(R.id.recy1);
        recy2 = findViewById(R.id.recy2);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(me,3);
        GridLayoutManager gridLayoutManager1 = new GridLayoutManager(me,4);
        GridLayoutManager gridLayoutManager2 = new GridLayoutManager(me,3);

        recy.setLayoutManager(gridLayoutManager);
        recy1.setLayoutManager(gridLayoutManager1);
        recy2.setLayoutManager(gridLayoutManager2);

        shaixuanAdapter = new ShaixuanAdapter(R.layout.item_shaixuan);
        recy.setAdapter(shaixuanAdapter);

        shaixuanAdapter1 = new ShaixuanAdapter1(R.layout.item_shaixuan1);
        recy1.setAdapter(shaixuanAdapter1);


        shaixuanAdapter2 = new ShaixuanAdapter2(R.layout.item_shaixuan2);
        recy2.setAdapter(shaixuanAdapter2);


    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }


    @Override
    public void initDatas(JumpParameter paramer) {

        String renqun = paramer.getString("renqun");
        String lian = paramer.getString("lian");
        String chang = paramer.getString("chang");


        if (!renqun.equals("")){
            String[] r = renqun.split(",");
            for (int i = 0; i < r.length; i++) {
                list1.add(r[i]);
            }
        }
        if (!lian.equals("")){
            String[] l = lian.split(",");
            for (int i = 0; i < l.length; i++) {
                list2.add(l[i]);
            }
        }

        if (!chang.equals("")){
            String[] c = chang.split(",");
            for (int i = 0; i < c.length; i++) {
                list3.add(c[i]);
            }
        }

        http();

    }


    private List<String> list1 = new ArrayList<>();
    private List<String> list2 = new ArrayList<>();
    private List<String> list3 = new ArrayList<>();

    private void http() {
        HttpRequest.POST(me, shaixuan, new Parameter(), new ResponseListener() {
            @Override
            public void onResponse(String response, Exception error) {
                if (error == null) {
                    Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);
                    if (map.get("code").equals("1")){

                        Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));

                        ArrayList<Map<String, String>> group = JSONUtils.parseKeyAndValueToMapList(data.get("group"));

                        for (int i = 0; i < group.size(); i++) {
                            group.get(i).put("flag","1");
                        }
                        if (list1.size() != 0){

                            for (int i = 0; i < list1.size(); i++) {

                                for (int i1 = 0; i1 < group.size(); i1++) {
                                    if (group.get(i1).get("id").equals(list1.get(i))){
                                        group.get(i1).put("flag","2");
                                    }
                                }

                            }
                        }


                        shaixuanAdapter.setNewData(group);

                        ArrayList<Map<String, String>> face = JSONUtils.parseKeyAndValueToMapList(data.get("face"));

                        for (int i = 0; i < face.size(); i++) {
                            face.get(i).put("flag","1");
                        }
                        if (list2.size() != 0){
                            for (int i = 0; i < list2.size(); i++) {

                                for (int i1 = 0; i1 < face.size(); i1++) {
                                    if (face.get(i1).get("id").equals(list2.get(i))){
                                        face.get(i1).put("flag","2");
                                    }
                                }

                            }
                        }
                        shaixuanAdapter1.setNewData(face);

                        ArrayList<Map<String, String>> length = JSONUtils.parseKeyAndValueToMapList(data.get("length"));

                        for (int i = 0; i < length.size(); i++) {
                            length.get(i).put("flag","1");
                        }
                        if (list3.size() != 0){
                            for (int i = 0; i < list3.size(); i++) {

                                for (int i1 = 0; i1 < length.size(); i1++) {
                                    if (length.get(i1).get("id").equals(list3.get(i))){
                                        length.get(i1).put("flag","2");
                                    }
                                }

                            }
                        }
                        shaixuanAdapter2.setNewData(length);
                    }
                }else {
                    toast("网络异常");
                }
            }
        });
    }

    @Override
    public void setEvents() {

        iv_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (list1.size() == 0 && list2.size() == 0 && list3.size() == 0){
                    setResponse(new JumpParameter()
                            .put("flag","111")
                            .put("list1", list1.toString().replaceAll("[\\{\\}\\[\\]]", "").replaceAll(" ", ""))
                            .put("list2", list2.toString().replaceAll("[\\{\\}\\[\\]]", "").replaceAll(" ", ""))
                            .put("list3", list3.toString().replaceAll("[\\{\\}\\[\\]]", "").replaceAll(" ", "")));
                    finish();
                }else {
                    setResponse(new JumpParameter()
                            .put("flag","999")
                            .put("list1", list1.toString().replaceAll("[\\{\\}\\[\\]]", "").replaceAll(" ", ""))
                            .put("list2", list2.toString().replaceAll("[\\{\\}\\[\\]]", "").replaceAll(" ", ""))
                            .put("list3", list3.toString().replaceAll("[\\{\\}\\[\\]]", "").replaceAll(" ", "")));
                    finish();
                }
            }
        });

        shaixuanAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<Map<String, String>> data = shaixuanAdapter.getData();

                if (data.get(position).get("flag").equals("2")){
                    data.get(position).put("flag","1");
                    for (int i = 0; i < list1.size(); i++) {
                        if (list1.get(i).equals(data.get(position).get("id"))){
                            list1.remove(i);
                        }
                    }
                }else {
                    data.get(position).put("flag","2");
                    list1.add(data.get(position).get("id"));
                }

                shaixuanAdapter.notifyDataSetChanged();
            }
        });
        shaixuanAdapter1.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<Map<String, String>> data = shaixuanAdapter1.getData();
                if (data.get(position).get("flag").equals("2")){
                    data.get(position).put("flag","1");
                    for (int i = 0; i < list2.size(); i++) {
                        if (list2.get(i).equals(data.get(position).get("id"))){
                            list2.remove(i);
                        }
                    }
                }else {
                    data.get(position).put("flag","2");
                    list2.add(data.get(position).get("id"));
                }

                shaixuanAdapter1.notifyDataSetChanged();
            }
        });
        shaixuanAdapter2.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<Map<String, String>> data = shaixuanAdapter2.getData();

                if (data.get(position).get("flag").equals("2")){
                    data.get(position).put("flag","1");
                    for (int i = 0; i < list3.size(); i++) {
                        if (list3.get(i).equals(data.get(position).get("id"))){
                            list3.remove(i);
                        }
                    }
                }else {
                    data.get(position).put("flag","2");
                    list3.add(data.get(position).get("id"));
                }

                shaixuanAdapter2.notifyDataSetChanged();
            }
        });

        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
